package com.ruoyi.web.controller.statistic;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.order.domain.StatisticCustomers;
import com.ruoyi.order.service.IStatisticCustomersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * VIEWController
 *
 * @author ruoyi
 * @date 2021-07-08
 */
@RestController
@RequestMapping("/system/customers")
@Api("用户报表统计")
public class StatisticCustomersController extends BaseController
{
    @Autowired
    private IStatisticCustomersService statisticCustomersService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询VIEW列表
     */
    @PreAuthorize("@ss.hasPermi('system:customers:list')")
    @GetMapping("/list")
    @ApiOperation("查询客户报表,不需要传参")
    public TableDataInfo list(StatisticCustomers statisticCustomers)
    {
        startPage();
        List<StatisticCustomers> list=null;
        SysUser user=tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<SysRole> roles = user.getRoles();
        for (SysRole role : roles) {
            if("超级管理员".equals(role.getRoleName())||"总经理".equals(role.getRoleName())||"财务采购经理".equals(role.getRoleName())||"销售经理".equals(role.getRoleName())){
                list = statisticCustomersService.selectStatisticCustomersList(statisticCustomers,tokenService.getLoginUser(ServletUtils.getRequest()).getUser());
                return getDataTable(list);
            }
            else {
                list = statisticCustomersService.selectStatisticCustomersListWithoutPrices(statisticCustomers, tokenService.getLoginUser(ServletUtils.getRequest()).getUser());
                return getDataTable(list);
            }
        }
        list=statisticCustomersService.selectStatisticCustomersList(statisticCustomers,tokenService.getLoginUser(ServletUtils.getRequest()).getUser());
        return getDataTable(list);
    }

    /**
     * 导出VIEW列表
     */
    @PreAuthorize("@ss.hasPermi('system:customers:export')")
    @Log(title = "VIEW", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出客户报表,不需要传参")
    public AjaxResult export(StatisticCustomers statisticCustomers)
    {
        List<StatisticCustomers> list = statisticCustomersService.selectStatisticCustomersList(statisticCustomers,tokenService.getLoginUser(ServletUtils.getRequest()).getUser());
        ExcelUtil<StatisticCustomers> util = new ExcelUtil<StatisticCustomers>(StatisticCustomers.class);
        return util.exportExcel(list, "VIEW数据");
    }

    /**
     * 获取VIEW详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:customers:query')")
    @GetMapping(value = "/{companyName}")
    public AjaxResult getInfo(@PathVariable("companyName") String companyName)
    {
        return AjaxResult.success(statisticCustomersService.selectStatisticCustomersById(companyName));
    }


}
